package imageviewer.servlet;

import java.awt.Image;
import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import imageviewer.beans.AuthorBean;
import imageviewer.beans.BasicBean;
import imageviewer.beans.DaysBean;
import imageviewer.beans.OverViewBean;
import imageviewer.beans.PicBean;
import imageviewer.domain.Camera;

@SuppressWarnings("serial")
public class ImageServlet extends HttpServlet{

	Image bild;
	Camera camera;
	Date datum;
	Date datum1;
	Timestamp uhrzeit1;
	Date datum2;
	Timestamp uhrzeit2;
	OverViewBean ovb = new OverViewBean();
	PicBean pb = new PicBean();

	public void init(ServletConfig conf) throws ServletException  {

		super.init(conf);

	}

	public void doPost (HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {

		HttpSession session = req.getSession();
		
		if (session == null) {

			res.sendRedirect("http://localhost:8080/error.html");
			
			return;

		}

		String action = req.getParameter("action");

		Object auth = session.getAttribute("auth");
		
		if(action == null)
			action = "";
		
		if(action.equals("Login")){

			String user = req.getParameter("user");

			String pass = req.getParameter("pass");

			AuthorBean bean = new AuthorBean();

			bean.setUsername(user);

			bean.setPassword(pass);

			bean.authorize();

			if(bean.isAuthorized()){

				session.setAttribute("auth", bean.getId());
				
				loadSelect(req,res);
				
			} else {
				
				req.setAttribute("AuthBean", bean);
				
				loadLogin(req, res);
			}

			return;

		} else if(auth == null){

			req.setAttribute("AuthBean", new AuthorBean());
			
			loadLogin(req,res);

			return;

		} else if( action.equals("Zeige Tag")){
			
			OverViewBean bean = new OverViewBean();
			
			bean.setDay(Date.valueOf(req.getParameter("day")));
			
			bean.setCameraIDs(req.getParameterValues("cameras"));
			
			bean.setUserID((Long)req.getSession().getAttribute("auth"));
			
			bean.fetchOverView();
			
			req.setAttribute("OverViewBean", bean);
			
			if(bean.isError()){
				
				loadSelect(req, res, bean);
				
			} else {
			
				loadImage(req, res);
			
			}
			
			return;
			
		} else if( action.equals("Zeige Von Bis")){
			OverViewBean bean = new OverViewBean();
			
			bean.setDayFrom(Date.valueOf(req.getParameter("dayFrom")));
			
			bean.setDayTo(Date.valueOf(req.getParameter("dayTo")));	
			
			bean.setTimeFrom(req.getParameter("TimeFrom"));
			
			bean.setTimeTo(req.getParameter("TimeTo"));
			
			bean.setCameraIDs(req.getParameterValues("cameras"));
			
			bean.setUserID((Long)req.getSession().getAttribute("auth"));
			
			bean.fetchOverView();
			
			req.setAttribute("OverViewBean", bean);
			
			if(bean.isError()){
				
				loadSelect(req, res, bean);
				
			} else {
				loadImage(req, res);
			
			}
			
			return;
			
		} else if (action.equals("Logout"))  {

			req.getSession().removeAttribute("auth");
			
			req.getSession().invalidate();
			
			req.setAttribute("AuthBean", new AuthorBean());
			
			loadLogin(req,res);
			
			return;

		} else {

			loadSelect(req,res);

			return;
		
		}

	}

	public void doGet (HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {

		doPost(req, res);

	}

	public void loadLogin(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{

		String url="/jsp/Login.jsp";

		ServletContext sc = getServletContext();

		RequestDispatcher rd = sc.getRequestDispatcher(url);

		rd.forward(req,res);
	}
	
	public void loadSelect(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
		loadSelect(req, res, new BasicBean());
	}
	
	public void loadSelect(HttpServletRequest req, HttpServletResponse res, BasicBean bbean) throws ServletException, IOException{

		String url="/jsp/Select.jsp";

		ServletContext sc = getServletContext();
		
		DaysBean bean = new DaysBean(bbean);
		
		bean.setUserId((Long)req.getSession().getAttribute("auth"));
		
		bean.getAllDays();
		
		req.setAttribute("DaysBean", bean);

		RequestDispatcher rd = sc.getRequestDispatcher(url);

		rd.forward(req,res);
	}

	public void loadImage(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{

		String url="/jsp/Image.jsp";

		ServletContext sc = getServletContext();

		RequestDispatcher rd = sc.getRequestDispatcher(url);

		rd.forward(req,res);
	}
}


